import {
  delay
} from '../../services/_utils/delay';
import {
  config
} from '../../config/index';
import {
  ds
} from '../../tools/data'

// 获取商品列表, tag指示标签 
function mockFetchGoodsList(pageIndex = 0, pageSize = 20, tag, cb) {


  const _ = ds.cmd
  const showTicket = (tag == '_ticket_')
  let whereObj = {
    _id: _.exists(true)
  }

  if (showTicket) { // 水票页面
    whereObj = {
      ticket: {
        gift: _.exists(true),
        deleted: _.exists(false).or(_.eq(false)).or(_.eq("false"))
      }
    }
  } else {
    whereObj = {
      tags: tag ? _.all([tag]) : undefined,
      visible: _.eq(true).or(_.eq("true"))
    }
  }

  ds.coll('goods')
    .where(whereObj)
    .orderBy("top", "desc")
    .orderBy("updateTime", "desc")
    .skip(pageIndex * pageSize)
    .limit(pageSize)
    .get()
    .then(res => {
      for (let item of res.data) {
        item.spuId = item._id.substr(8, 8);
        item.showTicket = showTicket
      }
      if (cb) cb(res.data)
    })

  return;
   {
    const {
      delay
    } = require('../_utils/delay');
    const {
      getGoodsList
    } = require('../../model/goods');
    return delay().then(() =>
      getGoodsList(pageIndex, pageSize).map((item) => {
        return {
          spuId: item.spuId,
          thumb: item.primaryImage,
          title: item.title,
          price: item.minSalePrice,
          originPrice: item.maxLinePrice,
          tags: item.spuTagList.map((tag) => tag.title),
        };
      }),
    );

    console.log(tag)
  }
}

/** 获取商品列表 */
export function fetchGoodsList(pageIndex = 1, pageSize = 20, tag, cb) {
  if (config.useMock) {
    return mockFetchGoodsList(pageIndex, pageSize, tag, cb);
  }
  return new Promise((resolve) => {
    resolve('real api');
  });
}